package com.api.framework.websocket;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
public class WsUser {
    /**
     * WebSocketUsers 日志控制器
     */
    private static final Logger logger = LoggerFactory.getLogger(WebSocketUsers.class);
    private static Map<String, Session> clients = new ConcurrentHashMap<String, Session>();

    /**
     * 存储用户
     *
     * @param key     唯一键
     * @param session 用户信息
     */
    public static void put(String key, Session session) {
        clients.put(key, session);
    }

    /**
     * 获取在线用户列表
     *
     * @return 返回用户集合
     */
    public static Map<String, Session> getClients() {
        return clients;
    }

    ;

    /**
     * 移除用户
     *
     * @param key 对接key
     * @return 移除结果
     */
    public static boolean removeAction(String key) {
//        String key = null;
//        boolean flag = clients.containsValue(session);
        boolean flag = clients.containsKey(key);
        if (flag) {
//            Set<Map.Entry<String, Session>> entries = clients.entrySet();
//            for (Map.Entry<String, Session> entry : entries) {
//                Session value = entry.getValue();
//                if (value.equals(session)) {
//                    key = entry.getKey();
//                    break;
//                }
//            }

        } else {
            return true;
        }
        return remove(key);
    }

    /**
     * 移出用户
     *
     * @param key 键
     */
    public static boolean remove(String key) {
        logger.info("\n 正在移出用户 - {}", key);
        Session remove = clients.remove(key);
        if (remove != null) {
            boolean containsValue = clients.containsValue(remove);
            logger.info("\n 移出结果 - {}", containsValue ? "失败" : "成功");
            return containsValue;
        } else {
            return true;
        }
    }

    public static boolean checkSession(String key) {
        return clients.containsKey(key);
    }


    public static Boolean sendMessageToUserByText(String key, String toKey, String message) {
        Boolean flag = clients.containsKey(toKey);
        try {
            if (flag) {
                clients.get(toKey).getBasicRemote().sendText(message);
                clients.get(key).getBasicRemote().sendText("已发送");
            } else {
                clients.get(key).getBasicRemote().sendText("发送对象以下线或者不存在");
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    /**
     * 发送文本消息
     *
     * @param session 自己的用户名
     * @param message 消息内容
     */
    public static Boolean returnMessage(Session session, String message) {
        if (session != null) {
            try {
                session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }
}

